import axios from 'axios'
import cfg from '../cfg/cfg'


// axios.defaults.withCredentials=true;
axios.defaults.headers['Content-Type'] = "application/x-www-form-urlencoded";
// axios.defaults.headers['Access-Control-Allow-Credentials'] = "true";
// axios.defaults.headers['Access-Control-Allow-Origin'] = "*";
// axios.defaults.headers['Access-Control-Allow-Headers'] = "X-Requested-With";
// axios.defaults.headers['Access-Control-Allow-Methods'] = "PUT,POST,GET,DELETE,OPTIONS";

export async function user_login(username, password) {
  let data = new URLSearchParams();
  data.append("username", username);
  data.append("password", password);
  return axios({
    method: "post",
    url: cfg.ApiUrl + "v1/user/login",
    data: data
  })
}

export async function user_reg(username, password) {
  let data = new URLSearchParams();
  data.append("username", username);
  data.append("password", password);
  return axios({
    method: "post",
    url: cfg.ApiUrl + "/v1/user/register",
    data: data
  })
}


export async function user_info(token) {
  let data = new URLSearchParams();
  data.append("token", token);
  return axios({
    method: "get",
    url: cfg.ApiUrl + "/v1/user/info",
    data: data,
    headers: {"Authorization": token}
  })
}


export async function user_out(token) {
  let data = new URLSearchParams();
  data.append("token", token);
  return axios({
    method: "get",
    url: cfg.ApiUrl + "/v1/user/out",
    data: data
  })
}

export async function user_reg_get(username) {
  let data = new URLSearchParams();
  data.append("username", username);
  return axios({
    method: "get",
    url: cfg.ApiUrl + "/v1/user/register/get",
    data: data
  })
}

export default {
  user_login,
  user_reg,
  user_info,
  user_out,
  user_reg_get
}
